import { ElLoading } from "@/components/loading"

export default (options) => {
    const vloading = ref(false)
    const value = unref(options)
    const { manual, ...loadingOptions } = typeof value === 'string' ? { text: value } : { ...value }
    let loadingRef

    const loading = () => {
        nextTick(() => {
            loadingRef = ElLoading.service(loadingOptions)
            vloading.value = true
        })
    }
    const close = () => {
        if (loadingRef) {
            loadingRef.close()
            vloading.value = false
        }
    }
    const setText = (text) => {
        if (loadingRef) {
            loadingRef.setText(text)
        }
    }

    const isloading = computed(() => {
        return vloading.value
    })

    if (!manual) {
        loading()
    }

    return { loading, close, setText, isloading }
}